var app = getApp()
import * as Api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasGoneAway: false,
    addresslist: [],
    model: false
  },
  changeModel() {

    this.setData({
      model: !this.data.model
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getaddressList() {
    console.log('获取地址')

    let self = this

    let url = app.globalData.baseUrl1 + '/address/list';

    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId
    }
    let callBack = (res) => {

      if (res == null) {

        console.log("网络请求错误！");
        return;
      }

      if (res.data.status != 200) {
        console.log("请求数据错误" + url);
        return;
      }
      if (res.data.status === 200) {
        console.log(res.data.data.addresslist.length)

        self.setData({
          addresslist: res.data.data.addresslist
        })
        if (res.data.data.addresslist.length > 0) {
          self.ifHaveDefauleAddress()
        }

      }
    }
    Api.reqFormPost(url, data, callBack)
  },
  //自动设置默认地址
  ifHaveDefauleAddress() {
    let self = this
    wx.request({
      method: 'post',
      url: app.globalData.baseUrl1 + '/address/defaultaddress',
      data: {
        token: app.globalData.token,
        userId: app.globalData.userId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(22, res.data.data)


        if (!res.data.data.address) {
          let e = {
            currentTarget: {
              dataset: {
                addressid: self.data.addresslist[0].addressId
              }
            }
          }
          self.setDefault(e)
        }

      }
    })

  },
  addEdit(e) {
let self = this
    if (!this.data.hasGoneAway) {

      switch (e.currentTarget.dataset.type) {
        case 'wxadd':
          wx.chooseAddress({
            success: function (res) {
              console.log()
              wx.navigateTo({
                url: '../add_edit/add_edit?contactname=' + res.userName +
                '&contactmobile=' + res.telNumber +
                '&provinceName=' + res.provinceName +
                '&cityName=' + res.cityName +
                '&countyName=' + res.countyName +
                '&address=' + res.detailInfo +
                '&pagetype=' + 'wxadd'
              })
            },complete(){
              self.setData({ hasGoneAway: false })
              
            }
          })
          break;
        case 'add':

          wx.navigateTo({
            url: '../add_edit/add_edit?pagetype=add'
          })
          break;
        case 'edit':
          console.log(e.currentTarget.dataset)

          wx.navigateTo({
            url: '../add_edit/add_edit?contactname=' + e.currentTarget.dataset.contactname +
            '&contactmobile=' + e.currentTarget.dataset.contactmobile +
            '&addressid=' + e.currentTarget.dataset.addressid +
            '&provinceid=' + e.currentTarget.dataset.provinceid +
            '&cityid=' + e.currentTarget.dataset.cityid +
            '&countyid=' + e.currentTarget.dataset.countyid +
            '&address=' + e.currentTarget.dataset.address +
            '&isdefault=' + e.currentTarget.dataset.isdefault +
            '&pagetype=edit'

          })

          break;
      }
    }
    this.setData({ hasGoneAway: true })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow);
      return
    }
    if (app.globalData.merchant.mustLogin && app.globalData.userId === null) {
      wx.navigateTo({
        url: '/pages/core/login/login',
        success() {
          wx.showToast({
            title: '请先登陆',
            icon: 'loading',
            duration: 1000
          })
        }
      })
      return
    }
    this.setData({ hasGoneAway: false })
    this.getaddressList()
  },
  deleteAddress(e) {
    let self = this
    wx.showModal({
      title: '提示',
      content: '确定删除该条收货地址？',
      success: function (res) {
        if (res.confirm) {
          let url = app.globalData.baseUrl1 + '/address/delete'
          let data = {
            token: app.globalData.token,
            userId: app.globalData.userId,
            addressId: e.currentTarget.dataset.addressid
          }
          let callBack = (res) => {

            if (res == null) {

              console.log("网络请求错误！");
              return;
            }

            if (res.data.status != 200) {
              console.log("请求数据错误");
              return;
            }
            if (res.data.status == 200) {

              self.getaddressList()

            }
          }
          Api.reqFormPost(url, data, callBack)

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })




  },
  setDefault(e) {

    let self = this

    let url = app.globalData.baseUrl1 + '/address/setdefault';

    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId,
      addressId: e.currentTarget.dataset.addressid
    }
    let callBack = (res) => {

      if (res == null) {

        console.log("网络请求错误！");
        return;
      }

      if (res.data.status != 200) {
        console.log("请求数据错误" + url);
        return;
      }

      if (res.data.status === 200) {
        self.getaddressList()
      }
    }
    Api.reqFormPost(url, data, callBack)


  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})